package command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import command.Impl.AddCollectionCommand;
import command.Impl.AddCustomerAddressCommand;
import command.Impl.AddOrderCommand;
import command.Impl.CancelCommand;
import command.Impl.DeleteCollectionCommand;
import command.Impl.DeleteCustomerAddressCommand;
import command.Impl.MakeSureCommand;
import command.Impl.PayCommand;
import command.Impl.RechargeCommand;
import command.Impl.SearchOrderCommand;
import command.Impl.ShowBalanceCommand;
import command.Impl.ShowCollectionCommand;
import command.Impl.ShowCustomerAddressCommand;
import pojo.Customer;

public class CustomerCommandFactory extends SystemCommandFactory {
	private static Map<CommandCode, Class<? extends Command>> mapCustomerCommand = new HashMap<>();

	static {
		mapCustomerCommand.put(CommandCode.RECHARGE, RechargeCommand.class);
		mapCustomerCommand.put(CommandCode.ADDCOLLECTION, AddCollectionCommand.class);
		mapCustomerCommand.put(CommandCode.ADDCUSTOMERADDRESS, AddCustomerAddressCommand.class);
		mapCustomerCommand.put(CommandCode.ADDORDER, AddOrderCommand.class);
		mapCustomerCommand.put(CommandCode.CANCEL, CancelCommand.class);
		mapCustomerCommand.put(CommandCode.DELETECOLLECTION, DeleteCollectionCommand.class);
		mapCustomerCommand.put(CommandCode.DELETECUSTOMERADDRESS, DeleteCustomerAddressCommand.class);
		mapCustomerCommand.put(CommandCode.SEARCHORDER, SearchOrderCommand.class);
		mapCustomerCommand.put(CommandCode.SHOWBALANCE, ShowBalanceCommand.class);
		mapCustomerCommand.put(CommandCode.SHOWCOLLECTIONS, ShowCollectionCommand.class);
		mapCustomerCommand.put(CommandCode.SHOWADDRESS, ShowCustomerAddressCommand.class);
		mapCustomerCommand.put(CommandCode.MAKESURE, MakeSureCommand.class);
		mapCustomerCommand.put(CommandCode.PAY, PayCommand.class);
	}

	protected Customer customer;

	public CustomerCommandFactory(Customer customer) {
		this.customer = customer;
	}

	public Command buildCommand(CommandCode code) {
		Command command = null;
		if (mapCustomerCommand.get(code) != null) {
			try {
				Constructor<? extends Command> cons = mapCustomerCommand.get(code).getConstructor(Customer.class);
				command = cons.newInstance(customer);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} else {
			command = super.buildCommand(code);
		}
		return command;
	}

}
